package com.eaoa.uc.customer.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.eaoa.base.EABaseAction;
import com.eaoa.constants.AttachmentConstants;
import com.eaoa.constants.PubAddressConstants;
import com.eaoa.constants.PubBillConstants;
import com.eaoa.query.pub.PubCityQuery;
import com.eaoa.query.pub.PubCustomerQuery;
import com.eaoa.uc.customer.form.CustomerForm;
import com.eaoa.utils.AttachmentUtils;
import com.eaoa.utils.ConstantsContainer;
import com.eaoa.vo.pub.PubCustomer;
import com.stony.core.keygenerator.FillCodeBO;
import com.stony.core.util.CoreUtils;

public class CustomerModifyPreAction extends EABaseAction {

	protected ActionForward doWork(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		CustomerForm myform = (CustomerForm) form;
		
		Long customerno = CoreUtils.parseLong(myform.getCustomerno());

		if (customerno!=null) {
			if (!isReEntry(request)) {
				PubCustomer po = PubCustomerQuery.loadByNo(customerno);
				myform.setVO(po);
			}
			Map attachments = AttachmentUtils.getAttachmentMap(AttachmentConstants.ATTACHMENTTYPE_CUSTOMER,customerno);
			request.setAttribute("attachments", attachments);
		}else{
			if (!isReEntry(request)) {
//				FillCodeBO codeBO = new FillCodeBO(PubBillConstants.BILLCODE_CUSTOMER);
//				codeBO.execute();
//			    myform.setCustomercode(codeBO.getNewCode());
			}
			
		}
		
		Long provinceno = CoreUtils.parseLong(myform.getAddressprovince());
		if (provinceno!=null){
			PubCityQuery cityQuery = new PubCityQuery();
			cityQuery.setIsLock("N");
			cityQuery.setParentno(provinceno);
			cityQuery.setLayer(new Long(2));	
			cityQuery.noCheckOutOfMemory();
			cityQuery.setOrder();
			cityQuery.execute();
			request.setAttribute("city", cityQuery.getResults());				
		}
		
		Long cityno = CoreUtils.parseLong(myform.getAddresscity());
		if (cityno!=null){
			PubCityQuery cityQuery = new PubCityQuery();
			cityQuery.setIsLock("N");
			cityQuery.setParentno(cityno);
			cityQuery.setLayer(new Long(3));	
			cityQuery.noCheckOutOfMemory();
			cityQuery.setOrder();
			cityQuery.execute();
			request.setAttribute("district", cityQuery.getResults());			
		}
		
		return mapping.findForward("success");

	}
}
